package com.cryptape.cita_wallet.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.alm.token.R;
import com.cryptape.cita_wallet.activity.transfer.TransferActivity;
import com.cryptape.cita_wallet.adapter.MyMinerAdapter;
import com.cryptape.cita_wallet.bean.MyHMIOOrderBean;
import com.cryptape.cita_wallet.bean.MyHMIOOrderItem;
import com.cryptape.cita_wallet.constant.ConstantUtil;
import com.cryptape.cita_wallet.item.Token;
import com.cryptape.cita_wallet.service.http.TokenService;
import com.cryptape.cita_wallet.view.SpacesItemDecoration;
import com.cryptape.cita_wallet.view.TitleBar;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/**
 * Created by dengming on 26/2/20.
 */

public class MyMinerActivity extends NBaseActivity{

    private RecyclerView recyclerView;
    private MyMinerAdapter minerAdapter;
    private TitleBar mTitle;
    private List<MyHMIOOrderItem> myMinerDatas = new ArrayList<>();

    @Override
    protected int getContentLayout() {
        return R.layout.activity_my_miner;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.my_miner_recycler);
        mTitle = findViewById(R.id.title_bar);
        mTitle.setOnRightClickListener(new TitleBar.OnRightClickListener() {
            @Override
            public void onRightClick() {
                startActivity(new Intent(MyMinerActivity.this,MinerIncomeActivity.class));
            }
        });
        mTitle.mTitleBarRightDefaultView.setCompoundDrawables(null,null,null,null);
    }

    @Override
    protected void initData() {
        initMinerData();
    }

    @Override
    protected void initAction() {

    }

    private void initMinerData() {
        initAdapter();
        showProgressBar();
        TokenService.getMyHMIOOrder().subscribe(new Subscriber<MyHMIOOrderBean>() {
            @Override
            public void onCompleted() {
                dismissProgressBar();
            }

            @Override
            public void onError(Throwable e) {
                dismissProgressBar();
                Toast.makeText(MyMinerActivity.this,getString(R.string.network_error),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(MyHMIOOrderBean data) {
                dismissProgressBar();
                if(data != null){
                    if(data.getCode() != 0){
                        return;
                    }
                    if(data.getData() != null){
                        myMinerDatas.clear();;
                        if(data.getData() != null) {
                            if(data.getData().getList() != null) {
                                myMinerDatas.addAll(data.getData().getList());
                            }
                        }
                        minerAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
//
//        TokenService.minerDetail().subscribe(new Subscriber<List<Type>>() {
//            @Override
//            public void onCompleted() {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                Toast.makeText(MyMinerActivity.this, getString(R.string.network_error), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onNext(List<Type> datas) {
//                if (datas != null && datas.size() > 3) {
//
//                    DynamicArray values =  (DynamicArray)datas.get(2);
//                    if(values == null){
//                        return;
//                    }
//                    List<Uint256> myMiners = values.getValue();
//                    if(myMiners == null || myMiners.size() == 0){
//                        return;
//                    }
//
//                    DynamicArray<Uint256> value0 = (DynamicArray)(datas.get(0));
//                    if(value0 == null){
//                        return;
//                    }
//                    List<Uint256> prices = value0.getValue();
//                    if(prices == null || prices.size() == 0){
//                        return;
//                    }
//                    DynamicArray<Uint256> value1 = (DynamicArray)(datas.get(1));
//                    if(value1 == null){
//                        return;
//                    }
//                    List<Uint256> totals = value0.getValue();
//                    if(totals == null || totals.size() == 0){
//                        return;
//                    }
//                    List<CloudMinerBean> miners = new ArrayList<>();
//                    int length = prices.size();
//                    for(int index = 0;index<length;index++)
//                    {
//                        CloudMinerBean bean = new CloudMinerBean();
//                        bean.setType(index);
//                        bean.setPrice(""+ NumberUtil.getEthFromWei(prices.get(index).getValue()));
//                        bean.setTotalAmount(totals.get(index).getValue().toString());
//                        miners.add(bean);
//                    }
//
//                    int num = myMiners.size();
//                    for(int index = 0; index < num; index++){
//                        int type = myMiners.get(index).getValue().intValue();
//                        int minerIdx = miners.indexOf(type);
//                        if(minerIdx != -1) {
//                            myMinerDatas.add(miners.get(minerIdx));
//                        }
//                    }
//                    minerAdapter.notifyDataSetChanged();
//                }
//            }
//        });
    }

    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new SpacesItemDecoration(10));

//        datas.add(new CloudMinerBean());
//        MyHMIOOrderItem item = new MyHMIOOrderItem();
//        item.setCalPower("1T");
//        item.setLevelName("M1");
//        item.setCreateTime("2019-1");
//        item.setStatus(1);
//        item.setStatusName("挖矿中");
//        item.setSurplusMoney("3000");
//        myMinerDatas.add(item);
        minerAdapter = new MyMinerAdapter(myMinerDatas);
        recyclerView.setAdapter(minerAdapter);

        minerAdapter.setOnItemClickListener((view, position) -> {
            Token token = new Token(ConstantUtil.ETH_MAINNET, ConstantUtil.HMIO_TOKEN_SYMBOL, ConstantUtil.ETHEREUM_MAIN_ID);
            Intent intent = new Intent(MyMinerActivity.this, MinerPayActivity.class);
            token.setContractAddress(ConstantUtil.getHToken());// "0xb2054aDd212F19581aEB195FDa7644471a996134";
            token.decimals = 18;
            intent.putExtra(TransferActivity.EXTRA_TOKEN, token);
            intent.putExtra(TransferActivity.EXTRA_ADDRESS, ConstantUtil.getMinerSellContract());
            startActivity(intent);
        });
    }
}
